import os
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext import db
import oauth, logging
import datastoreWrapper
from datastoreWrapper import Person

class LeaderboardHandler(webapp.RequestHandler):
  def get(self):
    score = self.request.get("score")
    cookie = self.request.headers.get('Cookie').split(';')
    for morsel in cookie:
        name,value = morsel.split('=')
        if name == 'memberId':
          memberId = value
    if not score:
      score = 0
    #Gets and updates score in database
    totalScore = datastoreWrapper.updateScore(memberId,score)
    topTen = datastoreWrapper.leaderboardQuery()
    for result in topTen:
	    logging.error(str(result.member_id) + " got high score: " + str(result.high_score))

    #return self.response.out.write(topTen)
    template_values = { 'leaders' : [{"first_name" : "Brett", "high_score" : 9001, "img" : "http://media.linkedin.com/mpr/mprx/0_iiTHlDxYfPGq-IzKX-D6luxyHCOnKsJKXT05ButYa_UnKsMCLQxHzH3jI9YZYa6OQK15rV1y2KM1L5o"},
                                                            {"first_name" : "David", "high_score" : 8500, "img" : "http://media.linkedin.com/mpr/mprx/0_iiTHlDxYfPGq-IzKX-D6luxyHCOnKsJKXT05ButYa_UnKsMCLQxHzH3jI9YZYa6OQK15rV1y2KM1L5o"},
                                                            {"first_name" : "Brian", "high_score" : 314, "img" : "http://media.linkedin.com/mpr/mprx/0_iiTHlDxYfPGq-IzKX-D6luxyHCOnKsJKXT05ButYa_UnKsMCLQxHzH3jI9YZYa6OQK15rV1y2KM1L5o"},
                                                            {"first_name" : "James", "high_score" : 1, "img" : "http://media.linkedin.com/mpr/mprx/0_iiTHlDxYfPGq-IzKX-D6luxyHCOnKsJKXT05ButYa_UnKsMCLQxHzH3jI9YZYa6OQK15rV1y2KM1L5o"}]}
    path = os.path.join(os.path.dirname(__file__), 'leader.html')

    self.response.out.write(template.render(path, {'leaders':topTen}))